<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ articlesList.title }}</span>

        <!-- 单图 -->
        <img
          class="thumb"
          v-if="articlesList.cover.type === 1"
          v-lazy="articlesList.cover.images[0]"
        />
      </div>

      <!-- 三张图片 -->
      <div class="thumb-box" v-if="articlesList.cover.type > 1">
        <img
          class="thumb"
          v-for="(imgURL, index) in articlesList.cover.images"
          :key="index"
          :src="imgURL"
        />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ articlesList.aut_name }}</span>
          <span>{{ articlesList.comm_count }}评论</span>
          <span>{{ formatDate(articlesList.pubdate) }}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" v-show="isShow" @click.stop="crossBtnFn" />
      </div>
    </template>
  </van-cell>
</template>

<script>
import { mapMutations } from 'vuex'

// 目标:文章Item里图片铺设
// 思路:先单图,，再多图
// 1.先写标签,写在哪里?，把数据弄进去
// 2.v-if="art0bj.cover.type === 1”就让单图出现>1，就让多图的容器出现
// 3．多图,v-for循环一个img标签显示即可

// 目标:日期插件(库)的dayjs的使用
// 1．找到官网
// 2．引入到工程内
// 3．查文档找到自己想要的功能,尝试使用
// 4．定义formatDate方法，接收要被转换的日期字符串,转换后返回到标签内显示

import * as dayjs from 'dayjs'
import * as relativeTime from 'dayjs/plugin/relativeTime' // import plugin
import 'dayjs/locale/zh-cn'
dayjs.extend(relativeTime) // use plugin
dayjs.locale('zh-cn') // use locale
export default {
  props: {
    articlesList: Object,
    isShow: {
      type: Boolean,
      default: true // 默认值 default
    }
  },
  methods: {
    ...mapMutations(['changeActionShowMutations', 'setActionArtIdMutations']),
    formatDate (dateStr) {
      var a = dayjs()
      var b = dayjs(dateStr)
      const c = a.to(b) // in 2 years
      // console.log(c)
      // return 返回值到函数调用的地方
      return c
    },
    crossBtnFn () {
      this.changeActionShowMutations(true)
      this.setActionArtIdMutations(this.articlesList.art_id)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
